<?php
/**
 * +----------------------------------------------------------------------
 * | Copyright © Sogu Technologies Co. Ltd 2018-2050 All rights reserved.
 * +----------------------------------------------------------------------
 * | File name: ImageCode.php
 * +----------------------------------------------------------------------
 * | Author: sam
 * +----------------------------------------------------------------------
 * | Description: 用于详细说明此程序文件完成的主要功能，与其他模块或函数的接口、输出值、取值范围、含义及参数间的控制、顺序或依赖等关系。
 * +----------------------------------------------------------------------
 * | Others: 其他内容的说明
 * +----------------------------------------------------------------------
 * | History: 修改历史记录列表，每条修改记录应包含修改日期、修改者及修改内容简述
 * +----------------------------------------------------------------------
 * | 1.Date: 2019-03-13 Author: sam Modification: 创建
 * +----------------------------------------------------------------------
 */

namespace App\Library\Tools;


use App\Constants\RedisKey;
use Illuminate\Support\Facades\Redis;
use Intervention\Image\Facades\Image;

class ImageCode
{
    public $img;

    public $width;

    public $height;

    public $bg_color;

    public function __construct($bg_color="#ccc",$width = '122.5',$height='35')
    {
        $this->width = $width;
        $this->height = $height;
        $this->bg_color = $bg_color;
        $this->img = Image::canvas($this->width,$this->height,$this->bg_color);
    }

    public function createImg($phone){
        $num1 = rand(1,9);
        $num2 = rand(1,9);
        $method = rand(0,2);
        switch ($method){
            case 0:
                $sign = '+';
                $result = $num1 + $num2;
                $y = 15;
                break;
            case 1:
                $sign = '-';
                $result = $num1 - $num2;
                $y = 20;
                break;
            case 2:
                $sign = 'x';
                $result = $num1 * $num2;
                $y = 15;
                break;
        }
        $this->makeText($num1,35,11);
        $this->makeText($sign,55,$y);
        $this->makeText($num2,75,11);
        $key = sprintf(RedisKey::IMG_CODE,$phone);
        Redis::setex($key,300,$result);
        $this->makePixel(200);
        return $this->img->response();
    }

    public function makeColor()
    {
        $str = '0123456789ABCDEF';
        $color = '#';
        $index = strlen($str)-1;
        for ($i = 0;$i<=5;$i++){
            $color .= $str[rand(0,$index)];
        }
        return $color;
    }

    public function makeText($str,$x,$y)
    {
        $color = $this->makeColor();
        $angle = 0;
        $this->img->text($str,$x,$y,function ($font)use($color,$angle){
            $font->file(dirname(dirname(dirname(__DIR__))) . '/font/Avenir.ttc');
            $font->size(24);
            $font->color($color);
            $font->align('center');
            $font->valign('top');
            $font->angle($angle);
        });
    }

    public function makePixel($num){
        for ($i = 0;$i<=$num;$i++){
            $color = $this->makeColor();
            $this->img->pixel($color, rand(1,$this->width), rand(1,$this->height));
        }
    }
}